package clock;

import model.Model;
import view.View;
/**
 * Syncronizes the model and view. Provides the general thread methods as well
 * as a pause function.
 *
 * @author joseph walker
 */
public class Clock extends Thread{
    private static class Ticks implements Runnable{
        private boolean pause;
        public Ticks(){ setPause(false);}
        public boolean isPaused(){ return pause;}
        public void pause(){setPause(!pause);}
        public void run(){
            while(!pause){
                try{
                    //System.out.println("\t\t\tTick");
                    model.tick();
                    view.tick();
                    Thread.sleep(1000);
                }
                catch(InterruptedException e){System.out.println("Interrupt");}
            }
        }
        private void setPause(boolean pause){this.pause = pause;}
    }
    private long delay = 1000;
    private static Model model;
    private static final Ticks tick = new Ticks();
    private static View view;
    public Clock(){
        super(tick);
    }
    /**
     * Pauses the clock.
     */
    public void pause(){
        tick.pause();
        if(!tick.isPaused()){run();}
    }
    @SuppressWarnings("static-access")
    public void setModel(Model model){
        this.model = model;
    }
    @SuppressWarnings("static-access")
    public void setView(View view){
        this.view = view;
    }
}
